import com.sun.source.tree.WhileLoopTree;

public class Work4 {
    public final static Object locker = new Object();
    static int state = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                ABCprinter("A",0);
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (locker) {
                ABCprinter("B",1);
            }
        });
        Thread t3 = new Thread(() -> {
            synchronized (locker) {
                ABCprinter("C",2);
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
    public static void ABCprinter(String letter,int targetState)  {
        for (int i = 0; i < 10; i++) {
            synchronized (locker) {
                while (state % 3 != targetState) {
                    try {
                        locker.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //state == targetState
                System.out.print(letter);
                if (targetState == 2) {
                    System.out.println();
                }
                state++;
                locker.notifyAll();
            }
        }
    }
}
